package com.bestcem.xm.wallet.util.business;

import com.bestcem.xm.wallet.config.properties.UserEmailProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 发送邮件工具类
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/5/14 20:38
 */
@Component
@Slf4j
public class MailUtil {

    /**
     * 超时时间
     */
    public static final String MAIL_SMTP_TIMEOUT = "mail.smtp.timeout";
    /**
     * 验证
     */
    public static final String MAIL_SMTP_AUTH = "mail.smtp.auth";
    /**
     * 类
     */
    public static final String MAIL_SMTP_SOCKET_FACTORY_CLASS = "mail.smtp.socketFactory.class";
    /**
     * 端口
     */
    public static final String MAIL_SMTP_SOCKET_FACTORY_PORT = "mail.smtp.socketFactory.port";

    @Autowired
    private UserEmailProperties userEmailProperties;

    private JavaMailSenderImpl walletMailSender;

    @PostConstruct
    public void walletMailSender() {
        walletMailSender = new JavaMailSenderImpl();
        walletMailSender.setProtocol(userEmailProperties.getSmtpProtocol());
        walletMailSender.setHost(userEmailProperties.getDefaultHost());
        walletMailSender.setUsername(userEmailProperties.getDefaultEmail());
        walletMailSender.setPassword(userEmailProperties.getDefaultPwd());
        walletMailSender.setDefaultEncoding(userEmailProperties.getSmtpEncoding());
        Properties properties = new Properties();
        // 设置超时时间
        properties.setProperty(MAIL_SMTP_TIMEOUT, userEmailProperties.getSmtpTimeout());
        // 如果是ssl的话,需要设置以下属性
        properties.setProperty(MAIL_SMTP_AUTH, userEmailProperties.getSmtpAuth());
        properties.setProperty(MAIL_SMTP_SOCKET_FACTORY_CLASS, userEmailProperties.getSmtpSocketFactoryClass());
        properties.setProperty(MAIL_SMTP_SOCKET_FACTORY_PORT, String.valueOf(userEmailProperties.getDefaultPort()));

        walletMailSender.setJavaMailProperties(properties);
    }

    /**
     * 发送邮件
     *
     * @param to      收件人
     * @param subject 主题
     * @param content 内容
     * @return boolean 发送结果
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/05/14 20:43
     */
    public boolean sendEmail(String[] to, String subject, String content) {
        try {
            MimeMessage message = walletMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(userEmailProperties.getDefaultEmail());
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            walletMailSender.send(message);
        } catch (Exception e) {
            if (log.isInfoEnabled()) {
                log.error("[USER] 发送邮件至{}异常，发件者: {}；{}", to, userEmailProperties.getDefaultEmail(), e.getMessage());
            }
            return false;
        }

        if (log.isInfoEnabled()) {
            log.info("[USER] 邮件已发送至{},发件者: {} ;", to, userEmailProperties.getDefaultEmail());
        }
        return true;
    }

}
